<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Web\BaseController;
use App\Services\Api\SendMessage;
use Illuminate\Http\Request;
use App\ConstDir\ErrorConst;
use Illuminate\Support\Facades\Mail;


class PublicApiController extends BaseController
{
    const EMAIL_LOSE_TIME = 5;

    /*
     * 公用短信发送接口
     */
    public function sendMessage(SendMessage $sendmessage , Request $request){

        $this->validate($request, [
            'phone' => 'required|string',
        ]);

        $data = $sendmessage->getMessage($request->input('phone'));

        if($data['code'] == 0){
            return $this->success($data['msg']);
        }else{
            return $this->error($data['msg'], $data['code']);
        }

    }


    /*
    * 检验短信验证码是否正确
    */
    public function verifyCode(Request $request){

        $this->validate($request, [
            'phone' => 'required|string',
            'code' => 'required|string'
        ]);
        $data = $request->all();
        $code = \Cache::get($data['phone']);
        if(!$code){
            return $this->error(ErrorConst::SMS_CODE_NO_MSG,ErrorConst::SMS_CODE_NO_CODE);
        }
        if($code != $data['code']){
            return $this->error(ErrorConst::SMS_CODE_ERROR_MSG,ErrorConst::SMS_CODE_ERROR_CODE);
        }else{
            return $this->success(ErrorConst::SUCCESS_CODE_MSG);
        }


    }


    /*
     * 邮件发送
     */
    public function sendEmail(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|string',
        ]);
        $sendcode=mt_rand(1000,9999);
        $toMail  = $request->input('email');
        $content="验证码:".$sendcode.",有效时间:".self::EMAIL_LOSE_TIME."分钟,请尽快完成验证";
        try {
            Mail::raw($content, function ($message) use ($toMail) {
                $message->subject('e井在线邮箱验证码发送');
                $message->to($toMail);
            });
        } catch (\Exception $e) {
            //return $this->error($e->getMessage());
            return $this->error(ErrorConst::BASE_ERROR_MSG);
        }

        if (Mail::failures()) {
            return $this->error(ErrorConst::FAILED_CODE_MSG);
        } else {
            \Cache::put($toMail,$sendcode,self::EMAIL_LOSE_TIME);
            return $this->success(ErrorConst::SUCCESS_CODE_MSG);
        }


    }


    /*
    * 检验邮件验证码是否正确
    */
    public function verifyEmailCode(Request $request){

        $this->validate($request, [
            'email' => 'required|string',
            'code' => 'required|string'
        ]);
        $data = $request->all();
        $code = \Cache::get($data['email']);
        if(!$code){
            return $this->error(ErrorConst::SMS_CODE_NO_MSG,ErrorConst::SMS_CODE_NO_CODE);
        }
        if($code != $data['code']){
            return $this->error(ErrorConst::SMS_CODE_ERROR_MSG,ErrorConst::SMS_CODE_ERROR_CODE);
        }else{
            return $this->success(ErrorConst::SUCCESS_CODE_MSG);
        }


    }


    /**
     * 配单短信发送接口
     */
    public function sendItemsMessage(SendMessage $sendmessage , Request $request){

        $this->validate($request, [
            'phone' => 'required|string',
        ]);

        $data = $sendmessage->getItemsMessage($request->input('phone'));

        if($data['code'] == 0){
            return $this->success($data['msg']);
        }else{
            return $this->error($data['msg'], $data['code']);
        }

    }




}
